package com.jeepig.evaluation.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.jeepig.common.core.domain.entity.SysDept;
import com.jeepig.common.utils.DateUtils;
import com.jeepig.common.utils.SecurityUtils;
import com.jeepig.common.utils.StringUtils;
import com.jeepig.evaluation.domain.*;
import com.jeepig.evaluation.mapper.*;
import com.jeepig.evaluation.vo.DeptEvalutionVo;
import com.jeepig.evaluation.vo.OverviewEvalutionVo;
import com.jeepig.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jeepig.evaluation.service.IMemberService;

/**
 * 待评价人员Service业务层处理
 *
 * @author guoxb
 * @date 2025-11-01
 */
@Service
public class MemberServiceImpl implements IMemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MenberEvaluationMapper menberEvaluationMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SelfEvaluationMapper selfEvaluationMapper;

    @Autowired
    private ComEvaluationMapper comEvaluationMapper;

    @Autowired
    private EvaluateMapper evaluateMapper;

    /**
     * 查询待评价人员
     *
     * @param id 待评价人员主键
     * @return 待评价人员
     */
    @Override
    public Member selectMemberById(String id) {
        return memberMapper.selectMemberById(id);
    }

    /**
     * 查询待评价人员列表
     *
     * @param member 待评价人员
     * @return 待评价人员
     */
    @Override
    public List<Member> selectMemberList(Member member) {
        return memberMapper.selectMemberList(member);
    }

    @Override
    public List<DeptEvalutionVo> selectDeptEvalutionVos(Long memberId, Long evaluateId) {
        MenberEvaluation menberEvaluation = new MenberEvaluation();
        menberEvaluation.setMemberId(memberId);
        menberEvaluation.setEvaluateId(evaluateId);
        List<MenberEvaluation> menberEvaluations = menberEvaluationMapper.selectMenberEvaluationList(menberEvaluation);
        List<DeptEvalutionVo> deptEvalutionVos = new ArrayList<>();
        for (MenberEvaluation me : menberEvaluations) {
            DeptEvalutionVo deptEvalutionVo = new DeptEvalutionVo();
            deptEvalutionVo.setMemberId(me.getMemberId());
            deptEvalutionVo.setEvaluteId(me.getEvaluateId());
            deptEvalutionVo.setDeptId(me.getDeptId());
            SysDept dept = sysDeptMapper.selectDeptById(me.getDeptId());
            if (dept != null) {
                deptEvalutionVo.setDeptName(dept.getDeptName());
            }
            deptEvalutionVo.setScore(me.getScore());
            deptEvalutionVo.setComment(me.getRemark());
            deptEvalutionVos.add(deptEvalutionVo);
        }
        return deptEvalutionVos;
    }

    @Override
    public List<OverviewEvalutionVo> selectOverviewEvalution(Long memberId) {
        List<OverviewEvalutionVo> overviewEvalutionVos = new ArrayList<>();
        List<Evaluate> evaluates = evaluateMapper.selectEvaluateList(new Evaluate());
        //查询单位评价
        List<MenberEvaluation> menberEvaluations = menberEvaluationMapper.selectMenberEvaluationByMemberId(memberId);
        Map<Long, List<MenberEvaluation>> menberEvaluationMap = new HashMap<>();
        for (MenberEvaluation menberEvaluation : menberEvaluations) {
            List<MenberEvaluation> tmpMenberEvaluations = menberEvaluationMap.get(menberEvaluation.getEvaluateId());
            if (tmpMenberEvaluations == null) {
                tmpMenberEvaluations = new ArrayList<>();
                tmpMenberEvaluations.add(menberEvaluation);
            } else {
                tmpMenberEvaluations.add(menberEvaluation);
            }
            menberEvaluationMap.put(menberEvaluation.getEvaluateId(), tmpMenberEvaluations);
        }
        //查询综合评价
        List<ComEvaluation> comEvaluations = comEvaluationMapper.selectComEvaluationByMemberId(memberId);
        Map<Long, ComEvaluation> comEvaluationMap = comEvaluations.stream().collect(Collectors.toMap(ComEvaluation::getEvalItemId, Function.identity()));
        //查询自我评价
        List<SelfEvaluation> selfEvaluations = selfEvaluationMapper.selectSelfEvaluationByMemberId(memberId);
        Map<Long, SelfEvaluation> selfEvaluationMap = selfEvaluations.stream().collect(Collectors.toMap(SelfEvaluation::getEvalItemId, Function.identity()));
        String unit = "个" + " ";
        for (Evaluate evaluate : evaluates) {
            OverviewEvalutionVo overviewEvalutionVo = new OverviewEvalutionVo();
            overviewEvalutionVo.setMemberId(memberId);
            overviewEvalutionVo.setEvaluateId(evaluate.getId());
            overviewEvalutionVo.setEvaluateName(evaluate.getSn() + "、" + evaluate.getName());
            ComEvaluation comEvaluation = comEvaluationMap.get(evaluate.getId());
            if (comEvaluation != null) {
                overviewEvalutionVo.setComScore(comEvaluation.getScore());
                overviewEvalutionVo.setComContent(comEvaluation.getContent());
            }
            SelfEvaluation selfEvaluation = selfEvaluationMap.get(evaluate.getId());
            if (selfEvaluation != null) {
                overviewEvalutionVo.setSelfScore(selfEvaluation.getScore());
                overviewEvalutionVo.setSelfContent(selfEvaluation.getContent());
            }
            List<MenberEvaluation> menberEvaluationList = menberEvaluationMap.get(evaluate.getId());
            if (StringUtils.isNotEmpty(menberEvaluationList)) {
                //A、B、C、D
                int a = 0, b = 0, c = 0, d = 0;
                for (MenberEvaluation me : menberEvaluationList) {
                    if (StringUtils.isNotEmpty(me.getScore())) {
                        switch (me.getScore()) {
                            case "A":
                                a++;
                                break;
                            case "B":
                                b++;
                                break;
                            case "C":
                                c++;
                                break;
                            case "D":
                                d++;
                                break;
                        }
                    }
                }
                StringBuilder stringBuilder = new StringBuilder();
                if (a != 0) {
                    stringBuilder.append("A").append(a).append(unit);
                }
                if (b != 0) {
                    stringBuilder.append("B").append(b).append(unit);
                }
                if (c != 0) {
                    stringBuilder.append("C").append(c).append(unit);
                }
                if (d != 0) {
                    stringBuilder.append("D").append(d).append(unit);
                }
                if(a == 0 && b == 0 && c == 0 && d == 0){
                    overviewEvalutionVo.setDeptContent("暂无评价");
                }else{
                    overviewEvalutionVo.setDeptContent(stringBuilder.toString());
                }
            }else{
                overviewEvalutionVo.setDeptContent("暂无评价");
            }
            overviewEvalutionVos.add(overviewEvalutionVo);
        }
        return overviewEvalutionVos;
    }

    /**
     * 新增待评价人员
     *
     * @param member 待评价人员
     * @return 结果
     */
    @Override
    public int insertMember(Member member) {
        member.setCreateBy(SecurityUtils.getUsername());
        member.setCreateTime(DateUtils.getNowDate());
        return memberMapper.insertMember(member);
    }

    /**
     * 修改待评价人员
     *
     * @param member 待评价人员
     * @return 结果
     */
    @Override
    public int updateMember(Member member) {
        member.setUpdateBy(SecurityUtils.getUsername());
        member.setUpdateTime(DateUtils.getNowDate());
        return memberMapper.updateMember(member);
    }

    /**
     * 批量删除待评价人员
     *
     * @param ids 需要删除的待评价人员主键
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(String[] ids) {
        return memberMapper.deleteMemberByIds(ids);
    }

    /**
     * 删除待评价人员信息
     *
     * @param id 待评价人员主键
     * @return 结果
     */
    @Override
    public int deleteMemberById(String id) {
        return memberMapper.deleteMemberById(id);
    }
}
